# -*- coding: utf-8 -*-
import KBEngine
import IKernel
import time
import random
import xml.dom.minidom
import ConstDef
import Functor
import MailDef
import MsgDef
import time
import datetime

from KBEDebug import *

class CapitalModule:
	def __init__(self):
		self.capitalsCfg = {}

	def init(self, isReload):
		self.loadRes()

	def loadRes(self):
		self.capitalsCfg = {}

		path = KBEngine.getResFullPath("cfg/capital.xml")
		#打开xml文档
		dom = xml.dom.minidom.parse(path)
		#得到文档元素对象
		root = dom.documentElement

		propEle = root.getElementsByTagName("property")
		for prop in propEle :
			type = int(prop.getAttribute("Type"));
			if type == 0 :
				continue

			capitalCfg = {}
			for eleName, v in prop._attrs.items():
				value = v.value
				if value.isdigit():
					value = int(v.value)
				capitalCfg[eleName] = value

			self.capitalsCfg[type] = capitalCfg

	def end(self):
		pass

	def getCapitalType(self, capitalName):
		"""
		通过资金名字获取资金类型
		@capitalName 资金的名字
		"""
		if isinstance(capitalName, str) == False:
			return 0

		for k, v in self.capitalsCfg.items():
			if v["Name"] != capitalName:
				continue
			return v["Type"]

		return 0

	def getCapitalName(self, capitalType):
		"""
		通过资金类型获取资金名字
		@capitalName 资金的类型
		"""
		if isinstance(capitalType, int) == False:
			return 0

		capitalCfg = self.capitalsCfg.get(capitalType)
		if capitalCfg == None:
			return ""

		return capitalCfg["Name"]

	def isVaildType(self, capitalType):
		"""
		是否是有效的资金类型
		"""
		return (self.capitalsCfg.get(capitalType) != None)

	def isVaildName(self, capitalName):
		"""
		是否是有效的资金名字
		"""
		for k, v in self.capitalsCfg.items():
			if v["Name"] == capitalName:
				return True

		return False

	def getNumByType(self, entityID, capitalType):
		"""
		通过资金类型获取对应实体的资金数量
		"""
		entity = KBEngine.entities.get(entityID)
		if entity == None:
			ERROR_MSG("entityID=%d no exist!" % (entityID))
			return False

		capital = entity.capitals.get(capitalType)
		if capital == None:
			return 0

		return capital["num"]

	def getNumByName(self, entityID, capitalName):
		"""
		通过资金名字获取对应实体的资金数量
		"""
		capitalType = self.getCapitalType(capitalName)
		return self.getNumByType(entityID, capitalType)

	def addByType(self, entityID, capitalType, addNum, logType):
		"""
		通过资金类型增加实体资金
		"""
		if addNum <= 0:
			ERROR_MSG("CapitalModule.addByType addNum <= 0")
			return False

		entity = KBEngine.entities.get(entityID)
		if entity == None:
			ERROR_MSG("entityID=%d no exist!" % (entityID))
			return False

		if self.isVaildType(capitalType) == False:
			return False

		capital = entity.capitals.get(capitalType)
		if capital == None:
			capitalCfg = self.capitalsCfg[capitalType]
			entity.capitals[capitalType] = {
				"name"		: capitalCfg["Name"],
				"num"		: 0,
				"type"		: capitalType
			}
			capital = entity.capitals.get(capitalType)

		capital["num"] = addNum + capital["num"]

		return True

	def addByName(self, entityID, capitalName, addNum, logType):
		"""
		通过资金名字增加实体资金
		"""
		capitalType = self.getCapitalType(capitalName)
		return self.addByType(entityID, capitalType, addNum, logType)

	def subByType(self, entityID, capitalType, subNum, logType):
		"""
		通过资金类型减少实体资金
		"""
		if subNum <= 0:
			ERROR_MSG("CapitalModule.addByType subNum <= 0")
			return False

		entity = KBEngine.entities.get(entityID)
		if entity == None:
			ERROR_MSG("entityID=%d no exist!" % (entityID))
			return False

		if self.isVaildType(capitalType) == False:
			return False

		capital = entity.capitals.get(capitalType)
		if capital == None:
			return False

		hasNum = capital["num"]
		if hasNum < subNum:
			return False

		capital["num"] = hasNum - subNum
		return True

	def subByName(self, entityID, capitalName, subNum, logType):
		"""
		通过资金名字减少实体资金
		"""
		capitalType = self.getCapitalType(capitalName)
		return self.subByType(entityID, capitalType, subNum, logType)








